package com.balabala.gym_goods.controller.admin;

import com.balabala.gym_enum.ResultEnum;
import com.balabala.gym_goods.pojo.entity.SpuType;
import com.balabala.gym_goods.service.SpuTypeService;
import com.balabala.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@Api(tags = "商品类型表接口")
@RequestMapping("/admin/spuType")
public class SpuTypeController {

    @Autowired
    private SpuTypeService spuTypeService;

    @ApiOperation(value = "查询所有商品类型信息")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Result findAll() {
        List<SpuType> spuTypeList=spuTypeService.list();
        return Result.ok(spuTypeList);
    }

    @ApiOperation(value = "根据id查询商品类型信息信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result find(@PathVariable("id") Long id) {
        return  Result.ok(ResultEnum.GENERALLY_SUCCESS,spuTypeService.getById(id));
    }

    @ApiOperation(value = "添加商品类型信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody SpuType spuType) {
        log.info("添加商品分类信息");
        spuType.setCreateTime(new Date());
        spuType.setUpdateTime(new Date());
        return Result.ok(spuTypeService.save(spuType));
    }

    @ApiOperation(value = "根据id删除商品类型信息")
    @RequestMapping (value = "/delete/{id}",method = RequestMethod.GET)
    public Result deleteById(@PathVariable Long id) {
        boolean result=spuTypeService.removeById(id);
        return Result.judge(result);
    }

    @ApiOperation(value = "修改商品类型信息")
    @RequestMapping (value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody SpuType spuType) {
        spuType.setUpdateTime(new Date());
        boolean result=spuTypeService.updateById(spuType);
        return Result.judge(result);
    }

}
